#include "bsp_wifi.h"
#include "cutil.h"
#include "bsp_uart.h"
#define UART_HANDLE huart1 //uart handle
#define debug_printf(fmt, ...) //UARTx_Printf(&huart3, fmt, ##__VA_ARGS__)
#define debug_printn(data, n) //UARTx_Printn(&huart3, data, n)
int bsp_wifi_uart_write(const uint8_t *data, uint16_t len);
int bsp_wifi_uart_printf(const char *fmt, ...);


esp_01s_mqtt_t wifi_module=
{
    .uart_tx = bsp_wifi_uart_write,
    .uart_tx_printf = bsp_wifi_uart_printf,
    .request_state = ESP_01S_REQ_IDLE_STATE,
    .receive_state = ESP_01S_RECV_IDLE_STATE
};



int bsp_wifi_uart_write(const uint8_t *data, uint16_t len)
{
    debug_printf("[WIFI]");
    debug_printn((uint8_t*)data, len);//debug
    return UARTx_Printn(&UART_HANDLE, (uint8_t*)data, len);
}

#define WIFI_MODULE_BUFFER_SIZE 128
uint8_t buffer_format[WIFI_MODULE_BUFFER_SIZE];
int bsp_wifi_uart_printf(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    int len = vsnprintf((char*)buffer_format, WIFI_MODULE_BUFFER_SIZE, fmt, args);
    debug_printf("[WIFI]");
    debug_printn((uint8_t*)buffer_format, len);//debug
    len = UARTx_Printn(&UART_HANDLE, buffer_format, len);
    va_end(args);
    return len;
}

int bsp_wifi_module_init(void)
{
    
	return 0;
}

int bsp_wifi_module_callback(uint8_t *pdata, uint16_t size)
{
    if (size == 0 || pdata == NULL) return -1;
    return esp01s_uart_rx_callback(&wifi_module, pdata, size);
}

void test_wifi_module(esp_01s_mqtt_t *pesp01s)
{
    // void esp01s_request_set_idle(esp_01s_mqtt_t *pesp01s);
    esp01s_reset(pesp01s);
    esp01s_get_wirless_mode_request(pesp01s);
    esp01s_set_wirless_mode(pesp01s, 1, 1);

    esp01s_get_ip_state_request(pesp01s);
    // esp01s_get_ip_state(const uint8_t *data, uint16_t size, char *ip, char *gateway, char *netmask);
    esp01s_ping(pesp01s, "192.168.1.1");

    esp01s_join_ap(pesp01s, "LiuPC", "00000000", NULL);
    esp01s_set_sap(pesp01s, "ESP_TEST", "00000000", 5, 3);

    esp01s_connect(pesp01s, 1, "192.168.1.1", 1883, 1883, NULL);
    esp01s_disconnect(pesp01s);
    esp01s_send(pesp01s, (uint8_t*)"[wifi_module]esp01s_send test\n\r", 31);
    esp01s_set_passthrough(pesp01s, 1);
    esp01s_set_passthrough(pesp01s, 0);
}

int bsp_wifi_test_sap(esp_01s_mqtt_t *pesp01s, int *state)
{
    if (*state == 0)
    {
        esp01s_reset(pesp01s);

    }
    else if (*state == 1)
    {
        esp01s_set_wirless_mode(pesp01s, 2, 1);

    }
    else if (*state == 2)
    {
        esp01s_set_sap(pesp01s, "hello world", "88888888", 5, 3);
    }
    (*state)++;
    if (*state == 3)
    {
        *state = 0;
    }
    return *state;
}

int bsp_wifi_test_jap(esp_01s_mqtt_t *pesp01s, int *state)
{
    if (*state == 0)
    {
        esp01s_reset(pesp01s);

    }
    else if (*state == 1)
    {
        esp01s_set_wirless_mode(pesp01s, 1, 1);

    }
    else if (*state == 2)
    {
        esp01s_join_ap(pesp01s, "LiuPC", "00000000", NULL);
    }
    (*state)++;
    if (*state == 3)
    {
        *state = 0;
    }
    return *state;
}

int esp01s_connect_test_callback(const uint8_t *pdata, uint16_t size)
{
    debug_printf("esp01s_connect_test_callback\n\r");
    debug_printf("[size:%d]", size);
    debug_printn((uint8_t*)pdata, size);
    debug_printf("\n\r");
	return 0;
}
int bsp_wifi_test_request(esp_01s_mqtt_t *pesp01s, int *state)
{
    if (*state == 0)
    {
        esp01s_get_wirless_mode_request(pesp01s);

    }
    else if (*state == 1)
    {
        esp01s_get_ip_state_request(pesp01s);
    }
    else if (*state == 2)
    {
        esp01s_ping(pesp01s, "192.168.137.1");
    }
    else if (*state == 3)
    {
        esp01s_connect(pesp01s, 1, "192.168.137.255", 8888, 8888, esp01s_connect_test_callback);
    }
    else if (*state == 4)
    {
        esp01s_set_transmit_mode(pesp01s, 1);
    }
    else if (*state == 5)
    {
        esp01s_set_passthrough(pesp01s, 1);
    }
    else if (*state == 6)
    {
        esp01s_send(pesp01s, (uint8_t*)"[wifi_module]esp01s_send test\n\r", 31);
    }
    else if (*state == 7)
    {
        esp01s_set_passthrough(pesp01s, 0);
    }
    else if (*state == 8)
    {
        esp01s_disconnect(pesp01s);
    }

    (*state)++;
    if (*state > 8)
    {
        *state = 0;
    }
    return *state;
    
}



